#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

class Solution 
{
public:
    int subarraySum(vector<int>& nums, int k) 
    {
        unordered_map<int, int> um;
        int count = 0;
        int sum = 0;
        for(int i = 0; i < nums.size(); i++)
        {
            sum += nums[i];
            if(sum == k)
            {
                count++;
            }
            auto it = um.find(sum - k);
            if(it != um.end())
            {
                count += it->second;
            }
            um[sum]++;
        }
        return count;
    }
};

// 辅助函数：打印数组
void printArray(const vector<int>& nums) 
{
    cout << "[";
    for(int i = 0; i < nums.size(); i++) 
    {
        cout << nums[i];
        if(i < nums.size() - 1) cout << ",";
    }
    cout << "]";
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [1,1,1], k = 2
    cout << "测试案例1:" << endl;
    vector<int> nums1 = {1, 1, 1};
    int k1 = 2;
    cout << "输入: nums = ";
    printArray(nums1);
    cout << ", k = " << k1 << endl;
    int result1 = solution.subarraySum(nums1, k1);
    cout << "输出: " << result1 << endl;
    cout << "期望: 2 (子数组 [1,1] 出现2次)" << endl << endl;
    
    // 测试案例2: [1,2,3], k = 3
    cout << "测试案例2:" << endl;
    vector<int> nums2 = {1, 2, 3};
    int k2 = 3;
    cout << "输入: nums = ";
    printArray(nums2);
    cout << ", k = " << k2 << endl;
    int result2 = solution.subarraySum(nums2, k2);
    cout << "输出: " << result2 << endl;
    cout << "期望: 2 (子数组 [3] 和 [1,2])" << endl << endl;
    
    // 测试案例3: 单个元素匹配
    cout << "测试案例3 (单个元素匹配):" << endl;
    vector<int> nums3 = {1};
    int k3 = 1;
    cout << "输入: nums = ";
    printArray(nums3);
    cout << ", k = " << k3 << endl;
    int result3 = solution.subarraySum(nums3, k3);
    cout << "输出: " << result3 << endl;
    cout << "期望: 1" << endl << endl;
    
    // 测试案例4: 单个元素不匹配
    cout << "测试案例4 (单个元素不匹配):" << endl;
    vector<int> nums4 = {1};
    int k4 = 0;
    cout << "输入: nums = ";
    printArray(nums4);
    cout << ", k = " << k4 << endl;
    int result4 = solution.subarraySum(nums4, k4);
    cout << "输出: " << result4 << endl;
    cout << "期望: 0" << endl << endl;
    
    // 测试案例5: 包含负数
    cout << "测试案例5 (包含负数):" << endl;
    vector<int> nums5 = {1, -1, 0};
    int k5 = 0;
    cout << "输入: nums = ";
    printArray(nums5);
    cout << ", k = " << k5 << endl;
    int result5 = solution.subarraySum(nums5, k5);
    cout << "输出: " << result5 << endl;
    cout << "期望: 3 (子数组 [1,-1], [0], [1,-1,0])" << endl << endl;
    
    // 测试案例6: 全负数
    cout << "测试案例6 (全负数):" << endl;
    vector<int> nums6 = {-1, -1, 1};
    int k6 = 0;
    cout << "输入: nums = ";
    printArray(nums6);
    cout << ", k = " << k6 << endl;
    int result6 = solution.subarraySum(nums6, k6);
    cout << "输出: " << result6 << endl;
    cout << "期望: 1 (子数组 [-1,-1,1])" << endl << endl;
    
    // 测试案例7: 大数组
    cout << "测试案例7 (大数组):" << endl;
    vector<int> nums7 = {1, 2, 1, 2, 1};
    int k7 = 3;
    cout << "输入: nums = ";
    printArray(nums7);
    cout << ", k = " << k7 << endl;
    int result7 = solution.subarraySum(nums7, k7);
    cout << "输出: " << result7 << endl;
    cout << "期望: 4 (子数组 [1,2], [2,1], [1,2], [2,1])" << endl << endl;
    
    // 测试案例8: k为0
    cout << "测试案例8 (k为0):" << endl;
    vector<int> nums8 = {0, 0, 0, 0, 0};
    int k8 = 0;
    cout << "输入: nums = ";
    printArray(nums8);
    cout << ", k = " << k8 << endl;
    int result8 = solution.subarraySum(nums8, k8);
    cout << "输出: " << result8 << endl;
    cout << "期望: 15 (所有子数组的和都是0)" << endl << endl;
    
    // 测试案例9: 无匹配
    cout << "测试案例9 (无匹配):" << endl;
    vector<int> nums9 = {1, 2, 3};
    int k9 = 7;
    cout << "输入: nums = ";
    printArray(nums9);
    cout << ", k = " << k9 << endl;
    int result9 = solution.subarraySum(nums9, k9);
    cout << "输出: " << result9 << endl;
    cout << "期望: 0" << endl << endl;
    
    // 测试案例10: 复杂情况
    cout << "测试案例10 (复杂情况):" << endl;
    vector<int> nums10 = {3, 4, 7, 2, -3, 1, 4, 2};
    int k10 = 7;
    cout << "输入: nums = ";
    printArray(nums10);
    cout << ", k = " << k10 << endl;
    int result10 = solution.subarraySum(nums10, k10);
    cout << "输出: " << result10 << endl;
    cout << "期望: 4 (子数组 [7], [3,4], [7,2,-3,1], [2,-3,1,4,2])" << endl << endl;
    
    return 0;
}